package com.bangdream.michelia.view.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bangdream.michelia.R;
import com.bangdream.michelia.entity.LiveBean;
import com.bangdream.michelia.utils.ImageLoadUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @date on 2018/6/18 15:04
 * @email 909732184@qq.com
 * 直播列表页的适配器
 */
public class LiveItemAdapter extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater layoutInflater;
    private OnViewItemClickListener onViewItemClickListener;
    private List<LiveBean> data;
    public OnViewItemClickListener getOnViewItemClickListener() {
        return onViewItemClickListener;
    }

    public void setOnViewItemClickListener(OnViewItemClickListener onViewItemClickListener) {
        this.onViewItemClickListener = onViewItemClickListener;
    }

    public void setData(List<LiveBean> data) {
        this.data = data;
    }

    public List<LiveBean> getData() {
        return data;
    }

    public interface OnViewItemClickListener{
        void onCurriculumItemClick(int position);
    }
    public LiveItemAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(layoutInflater.inflate(R.layout.live_adapter_item,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = null;
        if(holder instanceof ViewHolder){
            viewHolder = (ViewHolder) holder;
            viewHolder.position = position;
            LiveBean bean = data.get(position);
            LiveBean.LivePlayTeacherListBean teacherBean =
                    (bean.getLivePlayTeacherList()==null||bean.getLivePlayTeacherList().size()<1)?null:bean.getLivePlayTeacherList().get(0);

            ImageLoadUtils.ImageLoad(context,bean.getCover(),viewHolder.ivIcon,-1);
            viewHolder.tvTitle.setText(bean.getName()+"");
            viewHolder.tvText.setText(Html.fromHtml(bean.getContent())+"");
            if(bean.getTimeStateName()!=null&&bean.getTimeStateName().contains("结束")){
                viewHolder.tvState.setBackground(ContextCompat.getDrawable(context,R.drawable.cutting_gray_bg));
                viewHolder.tvStartTime.setText("下次直播时间："+bean.getStartTime());
            }else {
                viewHolder.tvStartTime.setText("开始时间："+bean.getStartTime());
                viewHolder.tvState.setBackground(ContextCompat.getDrawable(context,R.drawable.cutting_red_bg));
            }
            viewHolder.tvState.setText(bean.getTimeStateName()+"");
        }
    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private int position;
        private ImageView ivIcon;
        private TextView tvTitle;
        private TextView tvText;
        private TextView tvState;
        private TextView tvStartTime;
        public ViewHolder(View itemView) {
            super(itemView);
            ivIcon = itemView.findViewById(R.id.ivIcon);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvText = itemView.findViewById(R.id.tvText);
            tvState = itemView.findViewById(R.id.tvState);
            tvStartTime = itemView.findViewById(R.id.tvStartTime);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(onViewItemClickListener!=null){
                        onViewItemClickListener.onCurriculumItemClick(position);
                    }
                }
            });

        }
    }
}
